﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RTUParameterConfig.Class;

namespace RTUParameterConfig
{
    public partial class CurrentSpeedSetting : Form
    {
        public int intIndex;

        public CurrentSpeedSetting(int index)
        {
            InitializeComponent();
            intIndex = index;
            SetValue();
        }

        /// <summary>
        /// 设置流速仪配置
        /// </summary>
        public void SetValue()
        {
            try
            {
                if (intIndex == 0)
                {
                    nud1.Value = Convert.ToDecimal(BitConverter.ToInt32(ParameterConfig.rtuConfigInfo.CurrentSpeedDeviation1.Reverse<byte>().ToArray(), 0) / 1000.0);
                    nud2.Value = Convert.ToDecimal(BitConverter.ToUInt32(ParameterConfig.rtuConfigInfo.CurrentSpeedRatio1.Reverse<byte>().ToArray(), 0) / 10000.0);
                }

                if (intIndex == 1)
                {
                    nud1.Value = Convert.ToDecimal(BitConverter.ToInt32(ParameterConfig.rtuConfigInfo.CurrentSpeedDeviation2.Reverse<byte>().ToArray(), 0) / 1000.0);
                    nud2.Value = Convert.ToDecimal(BitConverter.ToUInt32(ParameterConfig.rtuConfigInfo.CurrentSpeedRatio2.Reverse<byte>().ToArray(), 0) / 10000.0);
                }
            }
            catch
            {
                Common.ShowErrorMessage("加载高级设置参数失败！");
            }
        }

        /// <summary>
        /// 获取流速仪配置
        /// </summary>
        public void GetValue()
        {
            if (intIndex == 0)
            {
                ParameterConfig.rtuConfigInfo.CurrentSpeedDeviation1 = BitConverter.GetBytes(Convert.ToInt32(nud1.Value * 1000)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.CurrentSpeedRatio1 = BitConverter.GetBytes(Convert.ToUInt32(nud2.Value * 10000)).Reverse<byte>().ToArray();
            }

            if (intIndex == 1)
            {
                ParameterConfig.rtuConfigInfo.CurrentSpeedDeviation2 = BitConverter.GetBytes(Convert.ToInt32(nud1.Value * 1000)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.CurrentSpeedRatio2 = BitConverter.GetBytes(Convert.ToUInt32(nud2.Value * 10000)).Reverse<byte>().ToArray();
            }
        }

        /// <summary>
        /// 关闭前保存修改的配置信息
        /// </summary>
        private void WaterLevelSetting_FormClosing(object sender, FormClosingEventArgs e)
        {
            GetValue();
            this.Dispose();
        }
    }
}
